<?php
require_once 'noticeurl.php';
function pageNavLink($totalpages, $pagenumber, $currentpage, $publication, $from, $to)
{
	if($pagenumber <= $totalpages) {
		if($currentpage != $pagenumber) {
	 		return "<a href=\"searchpublications.php?publication=".$publication."&from=".$from."&to=".$to."&page=".$pagenumber."\">".$pagenumber."</a>\n\t";	
		} else {
			return "<strong>[ ".$pagenumber." ]</strong>\n\t";
		}
	}
}
function insertSearchPageTopNavigation($publication, $from, $to, $totalpages, $currentpage)
{
    $startpage = $currentpage;
    if($currentpage < 3) {
        $startpage = 3;
    }

    $page1 = $startpage-2;
    $page2 = $startpage-1;
    $page3 = $startpage;
    $page4 = $startpage+1;
    $page5 = $startpage+2;

    echo "<li class=\"sortby\">";
    echo "<a href=\"searchpublications.php?publication=".$publication."&from=".$from."&to=".$to."&page=1\">&lt;&lt;</a>\n\t";
    echo pageNavLink($totalpages, $page1, $currentpage, $publication, $from, $to);
    echo pageNavLink($totalpages, $page2, $currentpage, $publication, $from, $to);
    echo pageNavLink($totalpages, $page3, $currentpage, $publication, $from, $to);
    echo pageNavLink($totalpages, $page4, $currentpage, $publication, $from, $to);
    echo pageNavLink($totalpages, $page5, $currentpage, $publication, $from, $to);
    echo "...\n\t";
    echo "<a href=\"searchpublications.php?publication=".$publication."&from=".$from."&to=".$to."&page=".$totalpages."\">".$totalpages."</a>\n\t";
    echo "<a href=\"searchpublications.php?publication=".$publication."&from=".$from."&to=".$to."&page=".$totalpages."\">&gt;&gt;</a>\n\t";
    echo "</li>";					
}
function insertSearchPageBottomNavigation($publication, $from, $to, $totalpages, $currentpage)
{
    $startpage = $currentpage;
    if($currentpage < 3) {
        $startpage = 3;
    }

    $page1 = $startpage-2;
    $page2 = $startpage-1;
    $page3 = $startpage;
    $page4 = $startpage+1;
    $page5 = $startpage+2;

    echo "<li class=\"sortby\">";
    echo "<a href=\"#topbar\" title=\"Back to Top\" class=\"leftfloat\">Back to Top</a>\n\t";
    echo "<a href=\"searchpublications.php?publication=".$publication."&from=".$from."&to=".$to."&page=1\">&lt;&lt;</a>\n\t";
    echo pageNavLink($totalpages, $page1, $currentpage, $publication, $from, $to);
    echo pageNavLink($totalpages, $page2, $currentpage, $publication, $from, $to);
    echo pageNavLink($totalpages, $page3, $currentpage, $publication, $from, $to);
    echo pageNavLink($totalpages, $page4, $currentpage, $publication, $from, $to);
    echo pageNavLink($totalpages, $page5, $currentpage, $publication, $from, $to);
    echo "...\n\t";
    echo "<a href=\"searchpublications.php?publication=".$publication."&from=".$from."&to=".$to."&page=".$totalpages."\">".$totalpages."</a>\n\t";
    echo "<a href=\"searchpublications.php?publication=".$publication."&from=".$from."&to=".$to."&page=".$totalpages."\">&gt;&gt;</a>\n\t";
    echo "</li>";
}
function convertDateTime($value)
{
    $blah = substr($value, 6, 4);     // bcdef
    $blah =  $blah."-".substr($value, 3, 2);
    $blah = $blah."-".substr($value, 0, 2);
    return $blah;
}
function getNewspaperName($pdfname, $dblink)
{
   $query = sprintf("SELECT `Name` FROM Newspapers WHERE `ShortName` = '%s'", $pdfname);
	 $result = mysql_query($query, $dblink);
	// Check result
	// This shows the actual query sent to MySQL, and the error. Useful for debugging.
	if (!$result) {
		$message  = 'Invalid query: ' . mysql_error() . "\n";
	    	$message .= 'Whole query: ' . $query;
    		die($message);
	}
    $value = "";
    if(mysql_num_rows($result)!=0) {
            $value = mysql_result($result, 0); // outputs third employee's name
    }
	return $value;
}
function showage($age)
{
	if($age > 0) {
		return "<strong>Age:</strong> ".$age;
	}
}
function formatdatetime($date)
{
	if($date == "0000-00-00 00:00:00") {
		return "";
	}   else {
		return date('d F Y', strtotime(str_replace("&", "&amp;", $date)));
	}
}
function showpublished($birthdate)
{
	if(strlen($birthdate) > 0) {
		return $birthdate;
	}
}
function showdeathdate($deathdate)
{
	if(strlen($deathdate) > 0) {
		return " <strong>d.</strong> ".$deathdate;
	}
}
function showformerly($former)
{
	if(strlen($former) > 0) {
		return " formerly ".$former; 
	}

}
function showaka($aka)
{
	if(strlen($aka) > 0) {
		return ". Also known as ".$aka."."; 
	}
	
}
function shownee($nee)
{
	if(strlen($nee) > 0) {
		return ", nee ".$nee;
	}
}
function showageextra($age, $extra)
{
	if(strlen(showage($age))> 0) {
		return $extra;
	}

}
function showfirstname($first)
{
	if(strlen($first) > 0) {
		return "-(".$first.")";
	}
}

$publication = $db->escape($_GET['publication']);
$fromDate = convertdateTime($db->escape($_GET['from']));
$toDate = convertdateTime($db->escape($_GET['to']));
$querycount = <<< EOT
    SELECT count(*)
    FROM Publication
    WHERE Publication.Name LIKE '%$publication%'
        AND Publication.DatePublished BETWEEN '$fromDate' AND '$toDate'
EOT;
$resultcount = mysql_query($querycount, $link);
$resultcount = mysql_result($resultcount, 0);

$query = <<< EOT
    SELECT Name, DatePublished, ID
    FROM Publication
    WHERE Publication.Name LIKE '%$publication%'
        AND Publication.DatePublished between '$fromDate' AND '$toDate'
    ORDER BY Publication.DatePublished ASC LIMIT
EOT;

$current = 0;
$totalpages = 1;
$resultsperpage = 3;
$totalpages = ceil($resultcount/$resultsperpage);
if($totalpages < 1) {
    $totalpages = 1;
}
$limitFrom = 0;
$to = $resultsperpage;
$currentpage = 1;
$page = intval($_GET['page']);
if(!empty($page)) {
	$currentpage = $page;
	$limitTo = $page*$resultsperpage;
	$limitFrom = $limitTo-$resultsperpage;
	$query .= ' '.$limitFrom.",".$resultsperpage;	

} else {
	$query .= ' 0,'.$resultsperpage;
}
$result = mysql_query($query, $link);

echo "<ul id=\"searchresults\">";

if (!empty($_GET['from']) && !empty($_GET['to']) && !empty($_GET['publication'])) {
    $from = $db->escape($_GET['from']);
    $to = $db->escape($_GET['to']);
    $publication = $db->escape($_GET['publication']);
    echo "<li class=\"header\">
    <h2>
    Newspaper Search Results for Death Notices in...<br /><span class=\"black\">".getNewspaperName($publication, $link)."<br />".formatdatetime(convertdateTime($from))." - ".formatdatetime(convertdateTime($to))."</span> 
    </h2>
    </li>"; //insert the actual publication name here.. will need to do a query to get that. 
    echo "<li class=\"resultinfo\">
      <span>(".$resultcount.") listing results &nbsp; (".$resultsperpage.") results per page &nbsp; (".$totalpages.") pages </span><a href=\"searchresults_newspapers_tips.php\">Search Tips</a>
      </li>";
    insertSearchPageTopNavigation($publication, $from, $to, $totalpages, $currentpage);
    if(mysql_num_rows($result)!=0) {
        while ($row = mysql_fetch_assoc($result)) {
            $query2 = "select Person.`ID`, `Surname`, `FirstNames` from Person left join PublicationPerson on Person.ID = PublicationPerson.PersonID where PublicationPerson.PublicationID = '".$row['ID']."' ORDER BY `Surname` ASC";
	    $result2 = mysql_query($query2, $link);

        $name = $row['Name'];
        $newspaperName = getNewspaperName($publication, $link);
        $newspaperNotices = getDeathNoticeURL($publication);
        if ($newspaperNotices) {
            $newspaperURL = $newspaperNotices['url'];
            $newspaperName = '<a href="'.$newspaperURL.'">'.$newspaperName.'</a>';
        }
        $datePublished = formatdatetime($row['DatePublished']);
        echo <<< EOT
        <li>
            <ul class="newspaperresults">
                <li>
                    <h3>$newspaperName - $datePublished</h3>
                    <ul class="twocolumn">

EOT;
	    if(mysql_num_rows($result2)!=0) {
                while ($row2 = mysql_fetch_assoc($result2)) {
                    if($row2['Surname'] != "ERROR"){
                        $personNotice = $html->text($row2['Surname']).", ".$html->text($row2['FirstNames'])." <a href=\"remembrance.php?personid=".$row2['ID']."\">view</a>";
                        echo "
                            <li>
                                $personNotice
                            </li>
";
                    }
                }
	    }

             echo <<< EOT
                    </ul>
                    <div style="clear:both;"></div>
                </li>
            </ul>
        </li>

EOT;
    }
    $current = $current+1;

  } else {
    echo "</ul><ul id=\"centerbar\"><li><p>Sorry but we have no newspaper notice for the date you have entered.</p>";
    echo "<p>Either no notice was published on that day or we have not yet received or processed* the notice. Please try again later.</p>";
    echo "<p>*receiving and processing notices can take up to 7 days from print date.</p></li>";
  }
  insertSearchPageBottomNavigation($publication, $from, $to, $totalpages, $currentpage);

} else {
  echo "</ul><ul id=\"centerbar\"><li><p>Sorry but we have no newspaper notice for the date you have entered.</p>";
  echo "<p>Either no notice was published on that day or we have not yet processed* the notice. Please try again later.</p>";
  echo "<p>It may also be possible that no public notice has been received or we have not yet processed* the notice.  Please try again later.</p>";
  echo "<p>*receiving and processing notices can take up to 7 days from print date.</p></li></ul>";
}

echo "</ul>";
?>
